/**
 * 
 */
package controllers;

import models.BanKuai;
import models.WenZhang;
import models.YongHu;
import play.cache.Cache;
import play.data.validation.Required;
import play.data.validation.Valid;
import play.i18n.Messages;
import play.libs.Codec;
import play.mvc.Controller;

/**
 * @author Administrator
 *
 */
public class TestViewer extends Controller{
    /**
     * 测试页面
     */
    public static void index(){
    	render();
    	//TODO 4、用户注册页面
    	//TODO 5、CRUD定制
    	
    }
    /**
     * 看板块页面
     * @param banKuaiMingCheng
     */
    public static void kanBanKuai(String banKuaiMingCheng){
    	render(banKuaiMingCheng);
    }
    /**
     * 看文章页面
     * @param wenZhangId
     */
    public static void kanWenZhang(Long wenZhangId,String huiTieNeiRong){
    	WenZhang wenZhang=WenZhang.findById(wenZhangId);
    	if(huiTieNeiRong!=null){
    		wenZhang.huiFu(Application.dangQianYongHu(),huiTieNeiRong);
    	}
		String banKuaiMingCheng=wenZhang.banKuai.mingCheng;
		render(banKuaiMingCheng,wenZhang);
    }

    /**
     * 登录检查
     * @param yongHuMing
     * @param miMa
     */
    public static void dengLuJianCha(String yongHuMing,String miMa){
    	YongHu yongHu=YongHu.lianJie(yongHuMing, miMa);
    	if(yongHu==null){
    		String yongHuMingCuoWu=Messages.get("dengLuJianCha.yongHuMingCuoWu");
    		String miMaCuoWu=Messages.get("dengLuJianCha.miMaCuoWu");
    		render(yongHuMingCuoWu,miMaCuoWu);
    	}else{
    		Application.yongHuKey=session.getId() + "-yongHu";
    		Cache.set(Application.yongHuKey, yongHu);
    		renderText("true");
    	}
    }
    public static void zhuXiao(){
    	session.clear();
    	Cache.clear();
    	index();
    }
    /**
     * 注册检查服务，用于客户端AJAX调用
     * @param yongHu
     */
    public static void zhuCeJianCha(@Valid YongHu yongHu,@Required(message="validation.equals.code.required") String code,String randomID){
    	System.out.println("code:"+code);
    	System.out.println("randomID:"+randomID);
    	validation.equals(code, Cache.get(randomID)
    	).message("validation.equals.code");
    	if(validation.hasErrors()) {
    		render();
    	}else{
    		Cache.set(randomID+"-jianCha","true", "1mn");
    		renderText("true");
    	}
    }
    public static void zhuCeYongHu(YongHu yongHu,String randomID){
    	System.out.println("zhuCeYongHu");
    	if(Cache.get(randomID+"-jianCha")!=null&&Cache.get(randomID+"-jianCha").equals("true")) {
            yongHu.create();
    		Application.yongHuKey=session.getId() + "-yongHu";
    		Cache.set(Application.yongHuKey, yongHu);
    		System.out.println("index...");
    		TestViewer.index();
        }else{
        	render("@zhuCe",yongHu);
        }
    	
		//TODO 这个注册用户action没有意义
    }
    public static void zhuCe(){
    	String randomID = Codec.UUID();
    	render(randomID);
    }
    public static void shuaXinYanZhengMa(){
    	String randomID = Codec.UUID();
    	renderText(randomID);
    }
	public static void dengLu(String yongHuMing, String miMa) {
		//如果是新打开登录页面，直接显示
		if(yongHuMing==null||miMa==null){
			render();
		}
		//否则就是已经打开登录页面，并提交了用户和密码，随后去尝试获取用户对象
		YongHu yongHu=YongHu.lianJie(yongHuMing, miMa);
		//如果用户对象找到了，就转到主页
		if(yongHu != null){
			Application.yongHuKey=session.getId() + "-yongHu";
			Cache.set(Application.yongHuKey, YongHu.findById(yongHu.id));
			index();
		}
		//如果用户对象没找到，把用户名和错误信息传入
//		flash.put("yongHuMing", yongHuMing);
//		flash.error(Messages.get("yongHuDengLu.error"));
//		render();
	}
}
